#include <iostream>
#include "List.h"

using namespace std;
namespace fs = boost::filesystem;

bool List::ispath() {
	bool what = true;
	if (!fs::is_directory(listpath)) {
		cout << "Not found: " << listpath << endl;
		what = false;
	}
	return what;
}

void List::create_ls(fs::path where, fs::path main) {
	string ls_name = main.string() + "/" + name;
	if (!ofile.is_open()) {
		ofile.open(ls_name.c_str(), ios::app);
	}
	if (fs::is_directory(where)) {
		fs::directory_iterator end_iter;
		for (fs::directory_iterator dir_iter(where); dir_iter != end_iter;
				++dir_iter) {
			try {
				if (fs::is_directory((*dir_iter).status())) {
					insidepath.push_back((*dir_iter).path().filename().string());
					create_ls((*dir_iter).path(), main);
					ofile.open(ls_name.c_str(), ios::app);
				} else {
					string pom = "";
					for (int i = 0; i < (int)insidepath.size(); i++) {
						ofile << '/' << insidepath[i];
						pom += '/' + insidepath[i];
					}
					if((*dir_iter).path().filename().string()!=name){
						ofile << '/' <<(*dir_iter).path().filename().string() << "\n";
						pom = '/'+(*dir_iter).path().filename().string();
					}
				}
			} catch (const exception & ex) {
				ofile << (*dir_iter).path().filename() << " " << ex.what()
						<< endl;
			}
		}
		if(!insidepath.empty()){
			insidepath.pop_back();
		}
		ofile.close();
	}
}

void List::createlist(){
	create_ls(listpath, listpath);
}
